/*
 * AlgorithmTest.java
 * JUnit based test
 *
 * Created on 20 October 2009, 9:55
 */

package diffview;

import junit.framework.*;
import java.util.*;
import java.io.*;
import static java.lang.Math.*;
import static java.lang.System.*;
import static diffview.Algorithm.*;

public class AlgorithmTest extends TestCase {
    
    public AlgorithmTest(String testName) {
        super(testName);
    }

    
    public void testSplit() {
        out.println("\n\n== split ==");
        assert (split("\n1\r\2\r\r34\n\r5\r\n6").equals
                (Arrays.asList(new String[] 
                        {"", "1", "2", "", "34", "", "5", "6"})));
    }

    
    public void testCreateModel() {
        out.println("\n\n== createModel ==");
        Algorithm alg = new Algorithm();
        String[] oldTexts = new String[] {
                "2\n3\n4\n5", 
                "1\n2\n3", 
                "",
                "",
                "1\n22",
                "1\n2\n3\r4\n5",
                "1\n2\n3\r4\n5"
            };
        String[] newTexts = new String[] {
                "1\n2\n3", 
                "2\n3\n4\n5", 
                "",
                "!",
                "22\n1",
                "1\n2\n4\r5",
                "1\n2\n6\r4\n5"
            };
        String[][] oldModelTexts = new String[][] {
                {null, "2", "3", "4", "5"},
                {"1", "2", "3", null, null},
                {""},
                {""},
                {"1", "22", null}, // weight(22) > weight(1)
                {"1", "2", "3", "4", "5"},
                {"1", "2", "3", "4", "5"}
            };
        String[][] newModelTexts = new String[][] {
                {"1", "2", "3", null, null},
                {null, "2", "3", "4", "5"},
                {""},
                {"!"},
                {null, "22", "1"},
                {"1", "2", null, "4", "5"},
                {"1", "2", "6", "4", "5"}
            };
        
        assert (oldTexts.length == oldModelTexts.length
                && newTexts.length == oldModelTexts.length
                && newTexts.length == newModelTexts.length);
        
        for (int i = 0; i < oldTexts.length; i++) {
            out.println("\n" + i + ": " + oldTexts[i] + ", " +  newTexts[i]);
            Model model = alg.createModel(oldTexts[i], newTexts[i]);
            assertEquals(model.getOldLines(), Arrays.asList(oldModelTexts[i]));
            assertEquals(model.getNewLines(), Arrays.asList(newModelTexts[i]));
        }
    }
    
}
